<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-col :span="12">
            <a-form-item label="机构" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-depart v-decorator="['office']" :trigger-change="true" :disabled='true'/>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="系统名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'systemName', validatorRules.systemName]" placeholder="请输入系统名称" readonly="readonly"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="申请的服务项目类型" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <j-multi-select-tag type="checkbox" v-decorator="['tos']" :trigger-change="true" dictCode="tos" :disabled='true' placeholder="请选择申请的服务项目类型"/>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="审核" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-decorator="[ 'type', {}]" disabled="disabled">
                <a-select-option value="1">等待审核</a-select-option>
                <a-select-option value="2">已通过</a-select-option>
                <a-select-option value="3">未通过</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="服务器申请附表" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="jinanGovernmentLedgerAttachedTable.loading"
            :columns="jinanGovernmentLedgerAttachedTable.columns"
            :dataSource="jinanGovernmentLedgerAttachedTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="false"
            @valueChange="handelChange"
          />
        </a-tab-pane>

      </a-tabs>

    </a-spin>
  </a-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import JEditableTable from '@/components/jeecg/JEditableTable'
  import JDate from '@/components/jeecg/JDate'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JMultiSelectTag from "@/components/dict/JMultiSelectTag"
  import JUpload from '@/components/jeecg/JUpload'
  import { httpAction, getAction } from '@/api/manage'

  export default {
    name: 'dredgeModal',
    mixins: [JEditableTableMixin],
    components: {
      JDate,
      JSelectDepart,
      JMultiSelectTag,
      JUpload,
      JEditableTable
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        disBool:true,
        disableSubmit:false,
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          updateTime:{},
          office:{},
          principal:{},
          principalPhone:{rules: [{validator: this.validatePhone}]},
          principalMail:{rules: [{validator: this.validateEmail}]},
          linkman:{},
          linkmanPhone:{rules: [{validator: this.validatePhone}]},
          linkmanMail:{rules: [{validator: this.validateEmail}]},
          systemName:{},
          mainUses:{},
          tos:{},
        },
        refKeys: ['jinanGovernmentLedgerAttached', ],
        tableKeys:['jinanGovernmentLedgerAttached', ],
        activeKey: 'jinanGovernmentLedgerAttached',
        // 服务器申请附表
        jinanGovernmentLedgerAttachedTable: {
          loading: false,
          dataSource: [],
          columns: [
            {  //这一个隐藏字段不能去掉，解决360浏览器不兼容问题
              title: '服务器名称',
              key: '',
              type: FormTypes.hidden,
              defaultValue: '',
              placeholder: '请输入${title}',
              align:"center",
            },
            {
              title: '服务器名称',
              key: 'serverName',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
              align:"center",
              disabled:"true",
            },
            // {
            //   title: '数量',
            //   key: 'num',
            //   type: FormTypes.inputNumber,
            //   defaultValue: '',
            //   placeholder: '请输入${title}',
            //   align:"center",
            //   disabled:"true",
            // },
            {
              title: 'CPU配置(核)',
              key: 'auditCpu',
              type: FormTypes.inputNumber,
              defaultValue: '',
              placeholder: '请输入${title}',
              align:"center",
              // dictCode:"cpu",
              disabled:"true",
            },
            {
              title: '审核内存(G)',
              key: 'auditMemory',
              type: FormTypes.inputNumber,
              defaultValue: '',
              placeholder: '请输入${title}',
              align:"center",
              // dictCode:"memory",
              disabled:"true",
            },
            {
              title: '审核云硬盘(G)',
              key: 'auditDisk',
              type: FormTypes.inputNumber,
              defaultValue: '',
              placeholder: '请输入${title}',
              align:"center",
              disabled:"true",
            },
            {
              title: '操作系统类型',
              key: 'os',
              type: FormTypes.select,
              defaultValue: '',
              placeholder: '请输入${title}',
              dictCode:'os',
              align:"center",
              disabled:"true",
              width:'10%'
            },
            {
              title: '数据库类类型',
              key: 'dataBase',
              type: FormTypes.select,
              defaultValue: '',
              placeholder: '请输入${title}',
              align:"center",
              dictCode:"jinan_data_base,dataBase_name,id",
              disabled:"true",
              width:'10%'
            },
            {
              title: '互联网IP需求',
              type: FormTypes.select,
              defaultValue: '',
              placeholder: '请输入${title}',
              key: 'outernetIp',
              options: [],
              dictCode: 'outernetIp',
              align:"center",
              disabled:"true",
              width:'7%',
              validateRules:[

              ]
            },
            {
              title: '备注',
              key: 'rest',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
              align:"center",
              disabled:"true",
            },
            {
              title: '开通时间',
              key: 'openTime',
              type: FormTypes.date,
              defaultValue: '',
              placeholder: '${title}',
              align:"center",
              width:'12%'
              //我想要在这里添加一个监听事件，在操作完成开通时间后出发方法，修改审核金额的值
            },
            {
              title: '分配IP地址',
              key: 'ipSite',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '${title}',
              align:"center",
              width:'12%'
            },
            {
              title: 'VPN',
              key: 'vpn',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '${title}',
              align:"center",
            },
            {
              title: '结算时间',
              key: 'thisPayTime',
              type: FormTypes.hidden,
              defaultValue: '',
              placeholder: '${title}',
              align:"center",
            },
            {
              title: '审核金额',
              key: 'auditMoney',
              type: FormTypes.hidden,
              defaultValue: '',
              placeholder: '',
              align:"center",
              disabled:"true",
            },
            {
              title: '申请金额',
              key: 'applyMoney',
              type: FormTypes.hidden,
              defaultValue: '',
              placeholder: '',
              align:"center",
              disabled:"true",
            },
            {
              title: '节省金额',
              key: 'saveMoney',
              type: FormTypes.hidden,
              defaultValue: '',
              placeholder: '',
              align:"center",
              disabled:"true",
            },
          ]
        },
        url: {
          // add: "/jinanServer/jinanGovernmentLedgerMain/add",
          edit: "/jinanDredge/dredgeMain/edit",
          jinanGovernmentLedgerAttached: {
            list: '/jinanServer/jinanGovernmentLedgerMain/queryJinanGovernmentLedgerAttachedByMainId'
          },
          getMoney:"/jinanDredge/dredgeMain/getMoney"
        }
      }
    },
    methods: {
      handelChange(event){
        const { type, row, column, value, target } = event;
        let openTime = row.openTime;
        if(openTime==""){
          let date = new Date();
          openTime = date.getFullYear()+"-"+date.getMonth()+"-"+date.getDay();
        }
        let thisPayTime = row.thisPayTime;
        if(thisPayTime==""){
          let date = new Date();
          thisPayTime = date.getFullYear()+"-12-31";
        }
        const obj = {rowId:row.id,date:openTime,ip:row.ipSite,thisPayTime:thisPayTime}
        switch (type) {
          case FormTypes.date :
            if(column.key=='openTime'){
              this.getMoney(obj,target,row,openTime);
            }
            if(column.key=='thisPayTime'){
              this.getMoney(obj,target,row,openTime);
            }
            break;
          case FormTypes.input:
            if(column.key=='ipSite'){
              this.getMoney(obj,target,row,openTime);
            }
            break;
        }
      },
      getMoney(params,target,row,openTime){
        getAction(this.url.getMoney, params).then(res => {
          target.setValues([{
              rowKey: row.id,
              values: { openTime: openTime,auditMoney: res.data.auditMoney, applyMoney: res.data.applyMoney,saveMoney:res.data.saveMoney }
            }])
        }).finally(() => {
        })
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'updateTime','office','principal','principalPhone','principalMail','linkman','linkmanPhone','linkmanMail','systemName','mainUses','tos','type','remarks'
        ,'openTime','ipSite','vpn','auditMoney','applyMoney','saveMoney', 'filePath','auditFilePath','open')
        this.$nextTick(() => {
          fieldval.type= this.disableSubmit?fieldval.type:"2";
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.jinanGovernmentLedgerAttached.list, params, this.jinanGovernmentLedgerAttachedTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          jinanGovernmentLedgerAttachedList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
      validateEmail(rule, value, callback){
        if(!value){
          callback()
        }else{
          if(new RegExp(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/).test(value)){
            callback()
          }else{
            callback("请输入正确格式的邮箱!")
          }
        }
      },
        //2020.4.22  14:56  雪糕修改正则表达式检验手机号输入格式（长度）。
        validatePhone(rule, value, callback){
            if(!value){
                callback()
            }else{
                if(new RegExp(/^1[3|4|5|7|8][0-9]\d{8}$/).test(value)){
                    callback()
                }else{
                    callback("请输入正确格式的手机号码!");
                }
            }
        },


    }
  }
</script>

<style scoped>
</style>